/*
 * @lc app=leetcode id=103 lang=cpp
 *
 * [103] Binary Tree Zigzag Level Order Traversal
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {

        vector<vector<int>> result;
        if (!root) {
            return result;
        }

        vector<TreeNode *> lv;
        lv.push_back(root);
        result.push_back({root->val});

        bool seq = true;
        while(!lv.empty()) {
            vector<TreeNode *> tmp;
            vector<int> level;
            for (auto n : lv) {
                if (n->left) {
                    tmp.push_back(n->left);
                }
                if (n->right) {
                    tmp.push_back(n->right);
                }
            }
            if (seq) {
                for (int i=tmp.size()-1; i>=0; i--) {
                    level.push_back(tmp[i]->val);
                }
            } else {
                for (int i=0; i<tmp.size(); i++) {
                    level.push_back(tmp[i]->val);
                }
            }

            seq = !seq;
            if (level.size() > 0)
                result.push_back(level);
            lv = tmp;
        }
        return result;
    }
};

